// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   ExcelServiceImp.java

package com.iphaeton.common.excel;

import com.iphaeton.exception.BusinessException;
import com.iphaeton.exception.PersistenceException;
import com.iphaeton.lms.business.PersistentServiceSupport;
import com.iphaeton.persistence.spring.JdbcPService;
import java.io.*;
import java.util.*;
import org.apache.poi.hssf.usermodel.*;

// Referenced classes of package com.iphaeton.common.excel:
//            IExcelService

public class ExcelServiceImp extends PersistentServiceSupport
    implements IExcelService
{

    public ExcelServiceImp()
    {
    }

    public List getDataConstruct(String t_name)
        throws BusinessException
    {
        List list = null;
        String sql = "sp_columns " + t_name;
        try
        {
            list = jdbcpservice.query(sql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u6570\u636E\u5E93\u8868\u7ED3\u6784\u65F6\u51FA\u9519!");
        }
        return list;
    }

    public List setColumnsName(List columnList, List nameList)
        throws BusinessException
    {
        List list = new LinkedList();
        if(columnList.size() != nameList.size())
            throw new BusinessException("\u6240\u5F55\u5165\u7684excel\u8868\u8868\u5934\u4E0E\u6570\u636E\u5E93\u5B57\u6BB5\u6570\u76EE\u4E0D\u540C!");
        for(int i = 0; i < columnList.size(); i++)
        {
            Map map = new HashMap();
            map.put(((Map)nameList.get(i)).get("name"), ((Map)columnList.get(i)).get("column_name"));
            list.add(map);
        }

        return list;
    }

    public HSSFWorkbook createExcel(String name, String url, List list)
        throws BusinessException
    {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("\u6570\u636E\u6A21\u677F");
        HSSFRow row = sheet.createRow(0);
        for(int i = 0; i < list.size(); i++)
        {
            HSSFCell cell = row.createCell((short)i);
            cell.setCellValue((String)((Map)list.get(i)).get("name"));
            cell.setCellType(((Integer)((Map)list.get(i)).get("type")).intValue());
        }

        return workbook;
    }

    public List readExcel(String excelName)
        throws BusinessException
    {
        HSSFWorkbook workbook;
        try
        {
            FileInputStream in = new FileInputStream(excelName);
            workbook = new HSSFWorkbook(in);
        }
        catch(FileNotFoundException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u4E0B\u8F7D\u6570\u636E\u6A21\u677F\u6587\u4EF6\u5931\u8D25,\u627E\u4E0D\u5230\u6307\u5B9A\u6587\u4EF6!");
        }
        catch(IOException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u4E0B\u8F7D\u6570\u636E\u6A21\u677F\u6587\u4EF6\u5931\u8D25,IO\u5F02\u5E38!");
        }
        HSSFSheet sheet = workbook.getSheetAt(0);
        List list = new LinkedList();
        for(int i = 2; i <= sheet.getLastRowNum(); i++)
        {
            HSSFRow row = sheet.getRow(i);
            List cellList = new LinkedList();
            for(int j = 0; j < row.getLastCellNum(); j++)
            {
                HSSFCell cell = row.getCell((short)j);
                if(cell != null)
                    switch(cell.getCellType())
                    {
                    case 0: // '\0'
                        cellList.add(new Double(cell.getNumericCellValue()));
                        break;

                    case 1: // '\001'
                        cellList.add(cell.getStringCellValue());
                        break;

                    default:
                        cellList.add("");
                        break;
                    }
                else
                    cellList.add(null);
            }

            list.add(cellList);
        }

        return list;
    }
}
